/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.PassengerNotifyInfo;
import com.sasis.dao.PassengerNotifyInfoDao;

import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.ibatis.SqlMapClientCallback;

public class PassengerNotifyInfoDaoiBatis extends BaseDaoiBATIS implements PassengerNotifyInfoDao {

    /**
     * @see com.sasis.dao.PassengerNotifyInfoDao#getPassengerNotifyInfos(com.sasis.model.PassengerNotifyInfo)
     */
    public List getPassengerNotifyInfos(final PassengerNotifyInfo passengerNotifyInfo) {
          return getSqlMapClientTemplate().queryForList("getPassengerNotifyInfos", passengerNotifyInfo);
    }

    /**
     * @see com.sasis.dao.PassengerNotifyInfoDao#getPassengerNotifyInfo(Integer id)
     */
    public PassengerNotifyInfo getPassengerNotifyInfo(Integer id) {
        PassengerNotifyInfo passengerNotifyInfo = (PassengerNotifyInfo) getSqlMapClientTemplate().queryForObject("getPassengerNotifyInfo", id);

        //if (passengerNotifyInfo == null) {
            //throw new ObjectRetrievalFailureException(PassengerNotifyInfo.class, id);
        //}

        return passengerNotifyInfo;
    }
    /**
     * @see com.sasis.dao.PassengerNotifyInfoDao#getPassengerNotifyInfo(Integer id)
     */
    public PassengerNotifyInfo getSearchPassengerNotifyInfo(Integer id) {
        PassengerNotifyInfo passengerNotifyInfo = (PassengerNotifyInfo) getSqlMapClientTemplate().queryForObject("getSearchPassengerNotifyInfo", id);

        //if (passengerNotifyInfo == null) {
            //throw new ObjectRetrievalFailureException(PassengerNotifyInfo.class, id);
        //}

        return passengerNotifyInfo;
    }

    /**
     * @see com.sasis.dao.PassengerNotifyInfoDao#savePassengerNotifyInfo(PassengerNotifyInfo passengerNotifyInfo)
     */    
    public int savePassengerNotifyInfo(final PassengerNotifyInfo passengerNotifyInfo) {
        Integer id = passengerNotifyInfo.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addPassengerNotifyInfo", passengerNotifyInfo);
        } else {
            getSqlMapClientTemplate().update("updatePassengerNotifyInfo", passengerNotifyInfo);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(PassengerNotifyInfo.class, id);
        }
        return id.intValue();
    }
    
    /**
     * @see com.sasis.dao.PassengerNotifyInfoDao#savePassengerNotifyInfos(PassengerNotifyInfo passengerNotifyInfo)
     */  
    //added by liuhao 2008-8-21 
    public void savePassengerNotifyInfos(final PassengerNotifyInfo passengerNotifyInfo) {
    	 final String[] ids=passengerNotifyInfo.getIds();
    	//String[]  exsistIds=null;
    	final int len = passengerNotifyInfo.getIds().length;
        
    	final String[] exsistIds = new String[len];
        // check for new record
        //if (null!=ids && 0!=ids.length) {
        	
             //  getSqlMapClientTemplate().update("updatePassengerNotifyInfos", passengerNotifyInfo);
        	//}else{
        	
        	       getSqlMapClientTemplate().execute(new SqlMapClientCallback() { 
        	    	   
                       public Object doInSqlMapClient(SqlMapExecutor executor)
                               throws SQLException { 
                           executor.startBatch();
                           int index=0;
                           String id="";
                           String passengerId="";
                           for(int i=0;i<len;i++)
                           {    
                        	   
                        	     //disjoin the id and passengerId
                      			 index=ids[i].indexOf(",");
                      			 if (index!=0){
                       			   id=ids[i].substring(0,index);
                       			   passengerId=ids[i].substring(index+1);
                       			 }else{
                       				 id="";
                       				 passengerId=ids[i].substring(1);
                       			 }
                      			 
                       			 passengerId=ids[i].substring(index+1);
                        	   
                        	   if(null!=id&&!"".equals(id)){
                        		   exsistIds[i]=id;
                        	   }else{
                        	     passengerNotifyInfo.setPi_id(new Integer(passengerId));	   
                                 executor.insert("addPassengerNotifyInfo", passengerNotifyInfo);
                        	   }
                           }
                           executor.executeBatch();
                           return null;
                       }
                   });
        	       if(null!=exsistIds)passengerNotifyInfo.setIds(exsistIds);
        	       getSqlMapClientTemplate().update("updatePassengerNotifyInfos", passengerNotifyInfo);
        	  // ids= (String[]) getSqlMapClientTemplate().insert("addPassengerNotifyInfos", passengerNotifyInfo);
        	       
        	//}
        
             
    }

    /**
     * @see com.sasis.dao.PassengerNotifyInfoDao#removePassengerNotifyInfo(Integer id)
     */
    public void removePassengerNotifyInfo(PassengerNotifyInfo passengerNotifyInfo) {
        getSqlMapClientTemplate().update("deletePassengerNotifyInfo",  passengerNotifyInfo);
    }
}
